// pages/bat_actor/bat_actor.js
var app=getApp()
var selected_devNo = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options, "options");
    let that = this;
    //selected_mid: "32,35,52"
    let mid_arr = [];
    that.setData({
      type: options.type
    });
    //单灯
    if(options.type == 0){
      mid_arr = options.selected_mid.split(',');
    }

    //支路
    if (options.type == 1) {
      mid_arr = options.branch_selected_mid.split(',');
    }
   

    //mid转换成dev
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    mid_arr.forEach(function(item, index, self){
      wx.request({
        url: 'https://119.3.46.145/server/FindSingleLight?id=' + item,
        method:'post',
        header:header,
        success:res=>{
          let data = JSON.parse(res.data.data)[0];
             //{"data":"[{\"LightType\":\"2\",\"MaxCurrent\":\"100.0\",\"turnOfftime\":\"05:00:00\",\"devNo\":\"868221043829715\",\"groupID\":\"1\",\"MinCurrent\":\"50.0\",\"Latitude\":\"32.3763\",\"Longitude\":\"119.387566\",\"Name\":\"无锡测试\",\"RatedPower\":\"0.0\",\"LightNo\":\"\",\"UserId\":\"922aeb81-29c8-41ba-9f82-36d9abb33265\",\"LightBrand\":\"\",\"id\":\"32\",\"turnOntime\":\"01:00:00\"}]"}   
          selected_devNo.push(data.devNo);
        },
        fail:error=>{
          console.log(error, "bat_actor FindSingleLight error");
        }
      })
    });    
  },



  saveTimeSettings:function(){
    let that = this;
    wx.navigateTo({
      url: '../setSwitchTime/setSwitchTime?selected_devNo=' + JSON.stringify(selected_devNo),
    }) 
  },

  saveCurrentSetting:function(){
    let that = this;
    wx.navigateTo({
      url: '../setCurrent/setCurrent?selected_devNo=' + JSON.stringify(selected_devNo),
    }) 
  },

  OPEN:function() {
    let that = this;
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }
    wx.request({
      url: "https://119.3.46.145/server/addMission",
      method: "post",
      header: header,
      data: {
        userid: app.globalData.userid,
        devNo: selected_devNo.join(','),
        type: 0
      },
      success: function (response) {
        let obj = response.data.error;
        if (obj == 0) {
          // alert("已加入任务队列，等待执行");
          // refreshTask();
          wx.showModal({
            title:'提示',
            content:'已加入任务队列，等待执行',
            showCancel:false
          });
        }
      }
    })
  },

  CLOSE:function() {
    let that = this;
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }
    wx.request({
      url: "https://119.3.46.145/server/addMission",
      method: "post",
      header: header,
      data: {
        userid: app.globalData.userid,
        devNo: selected_devNo.join(','),
        type: 1
      },
      success: function (response) {
        let obj = response.data.error;
        if (obj == 0) {
          // alert("已加入任务队列，等待执行");
          // refreshTask();
          wx.showModal({
            title: '提示',
            content: '已加入任务队列，等待执行',
            showCancel: false
          });
        }
      }
    })
  },

  AUTO:function() {
    let that = this;
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }
    wx.request({
      url: "https://119.3.46.145/server/addMission",
      method: "post",
      header: header,
      data: {
        userid: app.globalData.userid,
        devNo: selected_devNo.join(','),
        type: 2
      },
      success: function (response) {
        let obj = response.data.error;
        if (obj == 0) {
          // alert("已加入任务队列，等待执行");
          // refreshTask();
          wx.showModal({
            title: '提示',
            content: '已加入任务队列，等待执行',
            showCancel: false
          });
        }
      }
    })
  },

  MANUAL:function() {
    let that = this;
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }
    wx.request({
      url: "https://119.3.46.145/server/addMission",
      method: "post",
      header:header,
      data: {
        userid: app.globalData.userid,
        devNo: selected_devNo.join(','),
        type: 3
      },
      success: function (response) {
        let obj = response.data.error;
        if (obj == 0) {
          wx.showModal({
            title: '提示',
            content: '已加入任务队列，等待执行',
            showCancel: false
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})